package com.css.zfzx.fxpg.modules.workarea.repository.impl;

import com.css.zfzx.fxpg.modules.workarea.repository.FxWorkareaNativeRepository;
import com.css.zfzx.fxpg.modules.workarea.repository.entity.FxWorkareaEntity;
import com.css.zfzx.fxpg.modules.workarea.viewobjects.FxWorkareaQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-24
 */
@Repository
public class FxWorkareaNativeRepositoryImpl implements FxWorkareaNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxWorkareaNativeRepositoryImpl.class);


    @Override
    public Page<FxWorkareaEntity> queryFxWorkareas(FxWorkareaQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_workarea ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getWorkspace())) {
            whereSql.append(" and workspace like :workspace");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getMagnitude())) {
            whereSql.append(" and magnitude = :magnitude");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxWorkareaEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_workarea ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!PlatformObjectUtils.isEmpty(queryParams.getWorkspace())) {
            query.setParameter("workspace", "%" + queryParams.getWorkspace() + "%");
            countQuery.setParameter("workspace", "%" + queryParams.getWorkspace() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getMagnitude())) {
            query.setParameter("magnitude", queryParams.getMagnitude());
            countQuery.setParameter("magnitude", queryParams.getMagnitude());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxWorkareaEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxWorkareaEntity> fxWorkareaPage = new PageImpl<>(list, pageable, count.longValue());
        return fxWorkareaPage;
    }

    @Override
    public List<FxWorkareaEntity> listFxWorkareas(FxWorkareaQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_workarea ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getWorkspace())) {
            whereSql.append(" and workspace like :workspace");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getMagnitude())) {
            whereSql.append(" and magnitude = :magnitude");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxWorkareaEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getWorkspace())) {
            query.setParameter("workspace", "%" + queryParams.getWorkspace() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getMagnitude())) {
            query.setParameter("magnitude", queryParams.getMagnitude());
        }
        List<FxWorkareaEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<FxWorkareaEntity> listFxWorkareasForStatistic(FxWorkareaQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_workarea ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearStart()) &&
                !PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year >= :yearStart");
            whereSql.append(" and year <= :yearEnd");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearStart()) &&
                PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year >= :yearStart");
        }
        if(PlatformObjectUtils.isEmpty(queryParams.getYearStart()) &&
                !PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year <= :yearEnd");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxWorkareaEntity.class);
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearStart())) {
            query.setParameter("yearStart", queryParams.getYearStart());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            query.setParameter("yearEnd", queryParams.getYearEnd());
        }
        List<FxWorkareaEntity> list = query.getResultList();
        return list;
    }
}
